﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace ESAM_Assignment_3
{
	public partial class AddComment : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			string fullname = "";
			string name = Request.QueryString["username"];
			if (name == null)
			{
				Response.Redirect("Error.aspx");
			}
			else
			{
				fullname = UserClass.GetFullname(name);
			}
			if ((bool)Session[Common.ESAM_LOGIN] == false)
			{
				Response.Redirect("Default.aspx");
			}
			if ((string)Session[Common.ESAM_FULLNAME] != fullname)
			{
				Response.Redirect("Default.aspx?Username=" + Session["username"]);
			}
			if (!IsPostBack)
			{
				tbxComments.Enabled = true;
				tbxSubject.Enabled = true;
			}
		}

		protected void btnSubmit_Click(object sender, EventArgs e)
		{

			StringBuilder sb = new StringBuilder(HttpUtility.HtmlEncode(tbxComments.Text));

			sb.Replace("&lt;b&gt;", "<b>");
			sb.Replace("&lt;/b&gt;", "</b>");
			sb.Replace("&lt;i&gt;", "<i>");
			sb.Replace("&lt;/i&gt;", "</i>");

			string subject = tbxSubject.Text;
			string comment = sb.ToString();
			string username = Request.QueryString["Username"];
			string datePosted = Common.InsertTodayIntoCommentsDate();

			bool status = CommentClass.InsertComment(comment, username, datePosted, subject);

			if (status)
			{
				lblStatus.Text = "Comment added successfully.";
				lblStatus.ForeColor = System.Drawing.Color.Blue;
			}
			else
			{
				lblStatus.Text = "Comment failed to add!!!.";
				lblStatus.ForeColor = System.Drawing.Color.Red;
			}

			//Reset the textboxes
			tbxSubject.Text = "";
			tbxComments.Text = "";
		}
	}
}